package dongze.com.ckdj.data;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;
import javax.inject.Singleton;

import dongze.com.ckdj.dagger.component.AppComponent;
import dongze.com.ckdj.data.entity.request.AddVinMapRequestBody;
import dongze.com.ckdj.data.entity.request.BrandCodeRequestBody;
import dongze.com.ckdj.data.entity.request.EditMassLossRecordRequestBody;
import dongze.com.ckdj.data.entity.request.GetBrandsRequestBody;
import dongze.com.ckdj.data.entity.request.GetCheckInOperationSelectionRequestBody;
import dongze.com.ckdj.data.entity.request.GetCheckInRequestBody;
import dongze.com.ckdj.data.entity.request.GetNamePlateTodayRequest;
import dongze.com.ckdj.data.entity.request.GetNamePlateWorkLoadRequestBody;
import dongze.com.ckdj.data.entity.request.GetVehicleByNumberRequestBody;
import dongze.com.ckdj.data.entity.request.AddMassLossRecordRequestBody;
import dongze.com.ckdj.data.entity.request.InspectionSectionRecordDetailRequestBody;
import dongze.com.ckdj.data.entity.request.OperationsRequestBody;
import dongze.com.ckdj.data.entity.request.QueryChildPointsRequestBody;
import dongze.com.ckdj.data.entity.request.QueryMassLossRecordsRequestBody;
import dongze.com.ckdj.data.entity.request.QueryMassLossRequestBody;
import dongze.com.ckdj.data.entity.request.QueryMassLossTypesRequestBody;
import dongze.com.ckdj.data.entity.request.QueryOtherPointsRequestBody;
import dongze.com.ckdj.data.entity.request.RecordRequestBody;
import dongze.com.ckdj.data.entity.request.RequestHeader;
import dongze.com.ckdj.data.entity.request.RequestTemlate;
import dongze.com.ckdj.data.entity.request.UploadFileRequestBody;
import dongze.com.ckdj.data.entity.response.BrandResponse;
import dongze.com.ckdj.data.entity.response.GetCheckInRecordResponse;
import dongze.com.ckdj.data.entity.response.GetLaneResponse;
import dongze.com.ckdj.data.entity.response.GetNamePlateTodayResponse;
import dongze.com.ckdj.data.entity.response.GetNamePlateWorkLoadResponse;
import dongze.com.ckdj.data.entity.response.GetParkingAreaResponse;
import dongze.com.ckdj.data.entity.response.GetVinMapResponse;
import dongze.com.ckdj.data.entity.response.InsepctionSectionRecord;
import dongze.com.ckdj.data.entity.response.InspectionSectionRecordDetailResponse;
import dongze.com.ckdj.data.entity.response.InspectionSectionResponse;
import dongze.com.ckdj.data.entity.response.NamePlate;
import dongze.com.ckdj.data.entity.response.OperationsResponse;
import dongze.com.ckdj.data.entity.response.QueryChildPointsResponse;
import dongze.com.ckdj.data.entity.response.QueryMassLossRecordsResponse;
import dongze.com.ckdj.data.entity.response.QueryMassLossResponse;
import dongze.com.ckdj.data.entity.response.QueryMassLossTypesResponse;
import dongze.com.ckdj.data.entity.response.QueryOtherPointsResponse;
import dongze.com.ckdj.data.entity.response.ResponseTemplate;
import dongze.com.ckdj.data.entity.response.UploadImageResponse;
import dongze.com.ckdj.data.entity.response.User;
import dongze.com.ckdj.data.entity.request.UserRequestBody;
import dongze.com.ckdj.data.entity.request.VehicleNumberRequestBody;
import dongze.com.ckdj.data.entity.response.VehicleInformationDetail;
import dongze.com.ckdj.data.entity.response.VehicleNumberResponse;
import dongze.com.ckdj.data.gson.DoubleDefault0Adapter;
import dongze.com.ckdj.data.gson.GsonArrayNullAdapter;
import dongze.com.ckdj.data.gson.IntegerDefault0Adapter;
import dongze.com.ckdj.data.gson.LongDefault0Adapter;
import dongze.com.ckdj.data.webinterface.BackEndInterface;
import dongze.com.ckdj.data.webinterface.ImageUploadInterface;
import dongze.com.ckdj.util.MyLog;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Body;

/**
 * Created by Administrator on 2017/7/18.
 */

public class DataLayer {
    BackEndInterface backEndInterface;
    ImageUploadInterface imageUploadInterface;

    @Inject
    public DataLayer(BackEndInterface backEndInterface, ImageUploadInterface imageUploadInterface) {
        this.backEndInterface = backEndInterface;
        this.imageUploadInterface = imageUploadInterface;
    }

    private <T> RequestTemlate<T> buildRequest(String serviceCode, T t) {
        RequestTemlate<T> request = new RequestTemlate<>();
        RequestHeader header = new RequestHeader();
        header.setServiceCode(serviceCode);
        request.setHeader(header);
        request.setBody(t);
        return request;
    }

    public Call<ResponseTemplate<User>> login(UserRequestBody userRequestBody) {
        return backEndInterface.login(buildRequest("check_001", userRequestBody));
    }

    public Call<ResponseTemplate<List<VehicleNumberResponse>>> getVehicleNumber(String vehicleNumber, String brand, String voyage) {
        GetVehicleByNumberRequestBody v = new GetVehicleByNumberRequestBody();
        v.setVin(vehicleNumber);
        v.setBrand_code(brand);
        v.setVoyage(voyage);
        return backEndInterface.getVehichleByNumber(buildRequest("check_vin_001", v));
    }

    public Call<ResponseTemplate<VehicleInformationDetail>> getVehicleInfationDetail(String vehicleNumber, String nodeCode) {
        VehicleNumberRequestBody vehicleNumberRequestBody = new VehicleNumberRequestBody();
        vehicleNumberRequestBody.setVin(vehicleNumber);
        vehicleNumberRequestBody.setNode_code(nodeCode);
        return backEndInterface.getVehichleDetail(buildRequest("check_vin_002", vehicleNumberRequestBody));
    }

    public Call<ResponseTemplate<List<OperationsResponse>>> getOperations(String vehicleNumber, String sectionNode) {
        OperationsRequestBody body = new OperationsRequestBody();
        body.setNode_code(sectionNode);
        body.setVin(vehicleNumber);
        return backEndInterface.getOperations(buildRequest("check_program_001", body));
    }

    public Call<ResponseTemplate<List<QueryMassLossRecordsResponse>>> getMassLossRecords(String vinNo) {
        QueryMassLossRecordsRequestBody body = new QueryMassLossRecordsRequestBody();
        body.setVin(vinNo);
        return backEndInterface.getMassLossRecords(buildRequest("check_loss_getRecord", body));
    }

    public Call<ResponseTemplate<List<QueryMassLossTypesResponse>>> getMassLossTypes(String childId) {
        QueryMassLossTypesRequestBody body = new QueryMassLossTypesRequestBody();
        body.setPosition_id(childId);
        return backEndInterface.getMassLossTypes(buildRequest("check_loss_type_get", body));
    }

    public Call<ResponseTemplate<List<QueryOtherPointsResponse>>> getOtherPoints(String type, String brandCode) {
        QueryOtherPointsRequestBody body = new QueryOtherPointsRequestBody();
        body.setType(type);
        body.setBrand_code(brandCode);
        return backEndInterface.getOtherPoints(buildRequest("check_loss_extend_get", body));
    }

    public Call<ResponseTemplate<List<QueryChildPointsResponse>>> getPoints(String parentId) {
        QueryChildPointsRequestBody body = new QueryChildPointsRequestBody();
        body.setPosition_pid(parentId);
        return backEndInterface.getPoints(buildRequest("check_position_get", body));
    }

    public Call<ResponseTemplate<String>> addRecord(RecordRequestBody body) {
        return backEndInterface.addRecord(buildRequest("check_record_001", body));
    }

    public Call<UploadImageResponse> uploadImage(File image, UploadFileRequestBody.ProgressListener listener) {
        MultipartBody.Part body = MultipartBody.Part.createFormData("filedata", image.getName(),
                new UploadFileRequestBody(image, listener));
//                RequestBody.create(MediaType.parse("image/png"), image));
        return imageUploadInterface.uploadImage(body);
    }

    public Call<UploadImageResponse> uploadImage(File image) {
        MultipartBody.Part body = MultipartBody.Part.createFormData("filedata", image.getName(),
                RequestBody.create(MediaType.parse("multipart/form-data"), image));
        return imageUploadInterface.uploadImage(body);
    }

    public Call<ResponseTemplate<List<QueryMassLossResponse>>> queryMassLossReason(String reason) {
        QueryMassLossRequestBody body = new QueryMassLossRequestBody();
        body.setReason(reason);
        return backEndInterface.queryMassLossReason(buildRequest("check_loss_001", body));
    }

    public Call<ResponseTemplate<Long>> addMassLossRecord(AddMassLossRecordRequestBody body) {
        return backEndInterface.addMassLossRecord(buildRequest("check_loss_002", body));
    }

    public Call<ResponseTemplate<String>> updateMassLossImageRecord(String id, String imageID, String image, String user) {
        JsonObject jb = new JsonObject();
        jb.addProperty("id", id);
        jb.addProperty(imageID, image);
        jb.addProperty("uUser", user);
        MyLog.d("111", "updateMassLossImageRecord: id =" + id + ":" + image
                + ",imageID=" + imageID + "user=" + user);
        return backEndInterface.updateMassLossImageRecord(buildRequest("check_loss_003", jb));
    }

    public Call<ResponseTemplate<String>> updateMassLossImageRecord(EditMassLossRecordRequestBody body) {
        return backEndInterface.updateMassLossImageRecord(buildRequest("check_loss_003", new Gson().toJsonTree(body).getAsJsonObject()));
    }

    public Call<ResponseTemplate<List<InsepctionSectionRecord>>> getInspectionSectionRecords(String vin) {
        VehicleNumberRequestBody body = new VehicleNumberRequestBody();
        body.setVin(vin);
        return backEndInterface.getInspectionSectionRecords(buildRequest("check_record_002", body));
    }

    public Call<ResponseTemplate<List<InspectionSectionRecordDetailResponse>>> getInspectionSectionRecordDetail(String id) {
        InspectionSectionRecordDetailRequestBody body = new InspectionSectionRecordDetailRequestBody();
        body.setRecord_id(id);
        return backEndInterface.getInspectionSectionRecordDetail(buildRequest("check_record_003", body));
    }

    public Call<ResponseTemplate<List<BrandResponse>>> getBrands(String isChecked) {
        GetBrandsRequestBody brandsRequestBody = new GetBrandsRequestBody();
        brandsRequestBody.setIsCheck(isChecked);
        return backEndInterface.getBrands(buildRequest("check_carBrand", brandsRequestBody));
    }

    public Call<ResponseTemplate<List<InspectionSectionResponse>>> getInspectionSections(String brand) {
        BrandCodeRequestBody brandCodeRequestBody = new BrandCodeRequestBody();
        brandCodeRequestBody.setBrand_code(brand);
        return backEndInterface.getInspectionSections(buildRequest("check_nodeName", brandCodeRequestBody));
    }

    public Call<ResponseTemplate<NamePlate>> getNamePlate(String vin) {
        VehicleNumberRequestBody body = new VehicleNumberRequestBody();
        body.setVin(vin);
        return backEndInterface.getNamePlate(buildRequest("check_vinImg_get", body));
    }

    public Call<ResponseTemplate<String>> upLoadNamePlate(JsonObject jsonObject) {
        return backEndInterface.upLoadNamePlate(buildRequest("check_vinImg_save", jsonObject));
    }

    public Call<ResponseTemplate<GetNamePlateTodayResponse>> getNamePlateToday(String userNo) {
        GetNamePlateTodayRequest request = new GetNamePlateTodayRequest();
        request.setUserNo(userNo);
        return backEndInterface.getNamePlateToday(buildRequest("check_vinImg_userToday", request));
    }

    public Call<ResponseTemplate<List<String>>> getVoyage(String brandNo) {
        BrandCodeRequestBody brandCodeRequestBody = new BrandCodeRequestBody();
        brandCodeRequestBody.setBrand_code(brandNo);
        return backEndInterface.getVoyage(buildRequest("check_getVoyage", brandCodeRequestBody));
    }

    public Call<ResponseTemplate<GetNamePlateWorkLoadResponse>> getNamePlateWorkLoad(String brandCode, String voyage) {
        GetNamePlateWorkLoadRequestBody body1 = new GetNamePlateWorkLoadRequestBody();
        body1.setBrand_code(brandCode);
        body1.setVoyage(voyage);
        return backEndInterface.getNamePlateWorkLoad(buildRequest("check_vinImg_forVinList", body1));
    }

    public Call<ResponseTemplate<List<String>>> getCheckInOperationSelection(String key, String param, String type, String brandCode) {
        GetCheckInOperationSelectionRequestBody body = new GetCheckInOperationSelectionRequestBody();
        body.setPara_key(key);
        body.setPara(param);
        body.setSearch_type(type);
        body.setBrand_code(brandCode);
        return backEndInterface.getCheckInOperationSelection(buildRequest("check_getCarParas", body));
    }

    public Call<ResponseTemplate<List<GetParkingAreaResponse>>> getParkingArea() {
        return backEndInterface.getParkingArea(buildRequest("gj_getQuyu", ""));
    }

    public Call<ResponseTemplate<List<GetLaneResponse>>> getLane(String areaID) {
        JsonObject object = new JsonObject();
        object.addProperty("qyID", areaID);
        return backEndInterface.getLane(buildRequest("gj_getLane", object));
    }

    public Call<ResponseTemplate<String>> addVinMap(List<AddVinMapRequestBody> body) {
        return backEndInterface.addVinMap(buildRequest("gj_addVinMap", body));
    }

    public Call<ResponseTemplate<GetCheckInRecordResponse>> getEnclosureCheckInRecord(String nodeCode, String brandCode, String voyage) {
        GetCheckInRequestBody body = new GetCheckInRequestBody();
        body.setType("0");
        body.setBrand_code(brandCode);
        body.setNode_code(nodeCode);
        body.setVoyage(voyage);
        return backEndInterface.getCheckInRecord(buildRequest("check_recordsForTilVin", body));
    }

    public Call<ResponseTemplate<GetCheckInRecordResponse>> getOutLineCheckInRecord(String nodeCode, String brandCode, String voyage) {
        GetCheckInRequestBody body = new GetCheckInRequestBody();
        body.setType("1");
        body.setBrand_code(brandCode);
        body.setNode_code(nodeCode);
        body.setVoyage(voyage);
        return backEndInterface.getCheckInRecord(buildRequest("check_recordsForTilVin", body));
    }

    public Call<ResponseTemplate<String>> modifyCheckInRecord(String recordID, String value) {
        JsonObject object = new JsonObject();
        object.addProperty("record_detail_id", recordID);
        object.addProperty("program_value", value);
        return backEndInterface.modifyCheckInRecord(buildRequest("check_record_006", object));
    }

    public Call<ResponseTemplate<String>> deleteMassLossRecord(String body) {
        JsonObject object = new JsonObject();
        object.addProperty("id", body);
        return backEndInterface.deleteMassLossRecord(buildRequest("check_loss_004", object));
    }

    public Call<ResponseTemplate<String>> upLoadRandomInspection(JsonObject jsonObject) {
        return backEndInterface.upLoadRandomInspection(buildRequest("check_vinImgSj_save", jsonObject));
    }

    public Call<ResponseTemplate<List<QueryChildPointsResponse>>> getMassLossPositionID(JsonObject body) {
        return backEndInterface.getMassLossPositionID(buildRequest("check_positionId_get", body));
    }

    public Call<ResponseTemplate<List<GetVinMapResponse>>> getVinMap(@Body JsonObject body) {
        return backEndInterface.getVinMap(buildRequest("gj_getVinMap", body));
    }

    public Call<ResponseTemplate<String>> martinCommitCheckInImage(@Body JsonObject body) {
        return backEndInterface.martinCommitCheckInImage(buildRequest("martin_check_in", body));
    }

}
